package com.diyunapp.xilinapp.myindent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.TradeComplainBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ComplaintsaListActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.complaintsa_list)
    ListView complaintsaList;
    @Bind(R.id.complaintsa_refresh_view)
    SwipeRefreshLayout complaintsaRefreshView;
    private LayoutInflater inflater;
    private List<TradeComplainBean.DataBean> info = new ArrayList<>();
    private ComplainAdapter complainAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaintsa_list);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.complaintsa_menubar_inc  );
        Log.i("zhangxiaoxide ","com");
        super.MenuWidget(headView);
        complainAdapter=new ComplainAdapter(this);
        initcomplaintsa();
        complaintsaList.setAdapter(complainAdapter);
        complaintsaRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        complaintsaRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                initcomplaintsa();
            }
        });

    }
    private void initcomplaintsa() {
        String url = ""+ MainActivity.url+"act=goods&op=my_complain&member_id="+MainActivity.myLogin.getMemberid();
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        complaintsaRefreshView.setRefreshing(false);
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            info.clear();
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                            toast(String.valueOf(result));

                            if (result == 200) {
                                JSONArray listArray = resultObject.optJSONArray("data");
                                for (int i = 0; i < listArray.length(); i++) {
                                    JSONObject recordObject = listArray.optJSONObject(i);
                                    String complain_id=recordObject.optString("complain_id");
                                    String goods_name=recordObject.optString("goods_name");
                                    String accused_name=recordObject.optString("accused_name");
                                    String complain_subject_content=recordObject.optString("complain_subject_content");
                                    String complain_datetime=recordObject.optString("complain_datetime");
                                    String complain_state=recordObject.optString("complain_state");
                                    String goods_image=recordObject.optString("goods_image");
                                    info.add(new TradeComplainBean.DataBean(complain_id,goods_name,accused_name,complain_subject_content,
                                            complain_datetime,complain_state,goods_image));
//                                    Log.i("zhangxiaoxidebug", "resultinfo11=" + info);
//                                    Log.i("===============", "resultinfo=" + info);
                                }
                                complainAdapter.setList(info);
                                complainAdapter.notifyDataSetChanged();
                            } else {
                                toast("当前暂无数据");
//                                tuihuoRefreshView.setVisibility(View.GONE);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
//                            tuihuoRefreshView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        complaintsaRefreshView.setRefreshing(false);
                        toast("请求失败");

                    }
                });
    }
    class ComplainAdapter extends BaseAdapter {
        private List<TradeComplainBean.DataBean> info = new ArrayList<TradeComplainBean.DataBean>();
        private Context context;

        public ComplainAdapter(Context context) {
            super();
            this.context = context;
        }

        public ComplainAdapter() {

        }

        public void setList(List<TradeComplainBean.DataBean> list) {

            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return null == info ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return null == info ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (null == convertView) {
                convertView = inflater.inflate(R.layout.item_complaintsa, null);

                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final TradeComplainBean.DataBean bean = info.get(position);
            viewHolder.shopName.setText(bean.getAccused_name());
            String good_name=bean.getGoods_name();
            viewHolder.goodsIntroduce.setText(good_name);
            String state=bean.getComplain_state();
            if(state=="10"||state.equals("10")){
                viewHolder.intentState.setText("新投诉");
            }else if(state=="20"||state.equals("20")){
                viewHolder.intentState.setText("投诉通过转给被投诉人");
            }else if(state=="30"||state.equals("30")){
                viewHolder.intentState.setText("被投诉人已申诉");
            }else if(state=="40"||state.equals("40")){
                viewHolder.intentState.setText("提交仲裁");
            }else if(state=="99"||state.equals("99")){
                viewHolder.intentState.setText("已关闭");
            }
            Log.i("zhangxiaoxidebug",position+"+++"+good_name);

            if(good_name=="null"||good_name.equals("null")){
                viewHolder.goodsShow.setImageResource(R.drawable.list_default);
            }else {
                Log.i("zhangxiaoxidebug","===+++====");
                String image =bean.getGoods_image();
                Picasso.with(context)
                        .load(image)
                        .resize(200, 200)
                        .centerCrop()
                        .placeholder(R.drawable.list_default)
                        .error(R.drawable.list_default)
                        .into(viewHolder.goodsShow);
            }
            final String id=bean.getComplain_id();
            viewHolder.cancelBn.setTag(position);
            viewHolder.cancelBn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel(id);
                }
            });
            viewHolder.checkBtn.setTag(position);
            viewHolder.checkBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(ComplaintsaListActivity.this, TradeCompDetailActivity.class);
                    intent.putExtra("complain_id", id);
                    startActivity(intent);

                }
            });
            viewHolder.complain_type.setText(bean.getComplain_subject_content());

            return convertView;
        }

        class ViewHolder {
            @Bind(R.id.shop_name)
            TextView shopName;
            @Bind(R.id.intent_state)
            TextView intentState;
            @Bind(R.id.goods_show)
            ImageView goodsShow;
            @Bind(R.id.goods_introduce)
            TextView goodsIntroduce;
            @Bind(R.id.complain_type)
            TextView complain_type;

            @Bind(R.id.check_btn)
            Button checkBtn;
            @Bind(R.id.cancel_bn)
            Button cancelBn;
            @Bind(R.id.rel_goods_show)
            RelativeLayout rel_goods_show;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    private void cancel(String id) {
        String url=MainActivity.url+"act=goods&op=cancel_complain&complain_id="+id;
        Log.i("zhangxiaoxidebug","url---"+url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");

                            if (result == 200) {
                                String data = resultObject.optString("data");
                                toast(data);
                                initcomplaintsa();
                            } else {
                                String data = resultObject.optString("data");
                                toast(data);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                        toast("请求失败");

                    }
                });
    }


    @OnClick(R.id.go_back)
    public void onClick() {
        finish();
    }
}
